<?php
    ob_start();
    session_start();
    include("dbinfo.inc.php");
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die("Unable to select database");

    // get userID from the DB using the name
    $query="select ID from Account where userName like '$_POST[userName]';";
    $resultID=mysql_query($query);
    if (!$resultID) {
	echo "Error selecting ID from Account: " . mysql_error();
	die("");
    }
    $userID=mysql_result($resultID,0, "ID");

    $trailquery = "SELECT * FROM Trail WHERE";

    // Filter length
    // get avg length
    $query = "SELECT avg(length) FROM Filter WHERE userID=$userID";
    $result = mysql_query($query);
    $avg_length = mysql_result($result,0,"avg(length)");
    // get number of trail filters w/ specified length
    $query = "SELECT * FROM Filter WHERE length IS NOT NULL AND userID=$userID";
    $result = mysql_query($query);
    $spec_length = mysql_numrows($result);
    // get number of trail filters w/o specified length
    $query = "SELECT * FROM Filter WHERE length IS NULL AND 
		(highest_altitude IS NOT NULL OR difficulty IS NOT NULL) AND 
		userID=$userID;";
    $result = mysql_query($query);
    $any_length = mysql_numrows($result);

    if ($spec_length > $any_length) {
	$addAnd=1;
	if ($avg_length < 20) {
	    if ($avg_length < 10) { $length = 0; }
	    else { $length = 10; }
	    $trailquery.=" length <= ($length+10) AND
				length >= $length";
	}
 	else {
	    $length = 20;
	    $trailquery.=" length >= $length";
	}
	$_SESSION['length'] = $length;
    }
    else {
	$addAnd=0;
	$_SESSION['length'] = NULL;
    }

    // Filter highest_altitude
    // get avg hightest_altitude
    $query = "SELECT avg(hightest_altitude) FROM Filter WHERE userID=$userID";
    $result = mysql_query($query);
    $avg_hightest_altitude = mysql_result($result,0,"avg(hightest_altitude)");
    // get number of trail filters w/ specified hightest_altitude
    $query = "SELECT * FROM Filter WHERE hightest_altitude IS NOT NULL AND
		userID=$userID";
    $result = mysql_query($query);
    $spec_hightest_altitude = mysql_numrows($result);
    // get number of trail filters w/o specified hightest_altitude
    $query = "SELECT * FROM Filter WHERE hightest_altitude IS NULL AND 
		(length IS NOT NULL OR difficulty IS NOT NULL) AND 
		userID=$userID;";
    $result = mysql_query($query);
    $any_highest_altitude = mysql_numrows($result);

    if ($spec_highest_altitude > $any_highest_altitude) {
	if ($addAnd==1) { $query.=" AND"; }
	$addAnd=1;
	if ($avg_highest_altitude < 12000) {
	    if ($avg_highest_altitude < 3000) { $highest_altitude = 0; }
	    if ($avg_highest_altitude < 6000) { $highest_altitude = 3000; }
	    if ($avg_highest_altitude < 9000) { $highest_altitude = 6000; }
	    else { $highest_altitude = 9000; }
	    $trailquery.=" highest_altitude <= ($highest_altitude+3000) AND
				highest_altitude >= $highest_altitude";
	}
 	else {
	    $highest_altitude = 12000;
	    $trailquery.=" highest_altitude >= $highest_altitude";
	}
	$_SESSION['highest_altitude'] = $highest_altitude;
    }
    else {
	$addAnd=0;
	$_SESSION['highest_altitude'] = NULL;
    }

    // Filter difficulty
    // get avg difficulty
    $query = "SELECT avg(difficulty) FROM Filter WHERE userID=$userID";
    $result = mysql_query($query);
    $avg_difficulty = mysql_result($result,0,"avg(difficulty)");
    // get number of trail filters w/ specified difficulty
    $query = "SELECT * FROM Filter WHERE difficulty IS NOT NULL AND 
		userID=$userID";
    $result = mysql_query($query);
    $spec_difficulty = mysql_numrows($result);
    // get number of trail filters w/o specified difficulty
    $query = "SELECT * FROM Filter WHERE difficulty IS NULL AND 
		(length IS NOT NULL OR highest_altitude IS NOT NULL) AND 
		userID=$userID;";
    $result = mysql_query($query);
    $any_difficulty = mysql_numrows($result);

    if ($spec_difficulty > $any_difficulty) {
	if ($addAnd==1) { $query.=" AND"; }
	if ($avg_difficulty < 6) {
	    if ($avg_difficulty < 3) { $difficulty = 0; }
	    else { $difficulty = 3; }
	    $trailquery.=" difficulty <= ($difficulty+3) AND
				difficulty >= $difficulty";
	}
 	else {
	    $difficulty = 6;
	    $trailquery.=" difficulty >= $difficulty";
	}
	$_SESSION['difficulty'] = $difficulty;
    }
    else {
	$_SESSION['difficulty'] = NULL;
    }




    //$trailquery.=";";
    if (strstr($trailquery, '=') != FALSE) {
	$_SESSION['filterTrailQuery'] = $trailquery;
    }
    else {
	$_SESSION['filterTrailQuery'] = NULL;
    }



    mysql_close();
    session_write_close();
    header("Location: index.php");
    ob_flush();
?>
